home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Utilities / BindingUtils.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  9.4 KB  |  360 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        BindingUtils.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Ted Jucevic
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>      11/21/96    RA        Added ODSetPreferredKindProps
  13.          <3>      10/10/96    RA        Moved ChangeKindAndEditor, ChangeEditor,
  14.                                     IsNoPart and GetCurrentEditorForPart from
  15.                                     BindngH.cpp
  16.          <2>     10/7/96    TJ        Included TempObj.h
  17.  
  18.     To Do:
  19. */
  20.  
  21. /*
  22.     File:        BindingUtils.cpp
  23.  
  24.     Contains:    Binding utilities
  25.  
  26.     Owned by:    Reggie Adkins
  27.  
  28.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  29.         
  30. */
  31.  
  32. #ifndef SOM_ODBinding_xh
  33. #include <ODBindng.xh>
  34. #endif
  35.  
  36. #ifndef _TEMPOBJ_
  37. #include "TempObj.h"
  38. #endif
  39.  
  40. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  41. #include <StdTypes.xh>
  42. #endif
  43.  
  44. #ifndef _STDTYPIO_
  45. #include "StdTypIO.h"
  46. #endif
  47.  
  48. #ifndef _ISOSTR_
  49. #include "ISOStr.h"
  50. #endif
  51.  
  52. #ifndef _BNDNSUTL_
  53. #include "BndNSUtl.h"
  54. #endif
  55.  
  56. #ifndef _BINDINGUTILS_
  57. #include "BindingUtils.h"
  58. #endif
  59.  
  60. #ifndef _INFOUTIL_
  61. #include "InfoUtil.h"
  62. #endif
  63.  
  64. #ifndef _STORUTIL_
  65. #include "StorUtil.h"
  66. #endif
  67.  
  68. #ifndef SOM_ODPartWrapper_xh
  69. #include <PartWrap.xh>
  70. #endif
  71.  
  72. #undef LOGGING
  73. #define LOGGING 0
  74.  
  75. //------------------------------------------------------------------------------
  76. // IsNoPart
  77. //------------------------------------------------------------------------------
  78.  
  79. ODBoolean IsNoPart(Environment* ev, ODPart* part)
  80. {
  81.     WASSERT(part);
  82.     
  83.     ODBoolean result = kODFalse;
  84.  
  85.     if (part)
  86.     {
  87.         TempODEditor editor = ((ODPartWrapper*)part)->GetEditor(ev);
  88.         
  89.         ODUShort length = (ODUShort)ODISOStrLength(editor);
  90.         result = ODISOStrNCompare(editor, kODBlackBoxHandlerOfLastResort, length) == 0;
  91.     }
  92.     
  93.     return result;
  94. }
  95.  
  96. //------------------------------------------------------------------------------
  97. // IsNoPart
  98. //------------------------------------------------------------------------------
  99.  
  100. ODBoolean IsNoPart(Environment* ev, ODEditor editor)
  101. {
  102.     ODUShort length = (ODUShort)ODISOStrLength(editor);
  103.     
  104.     return (ODISOStrNCompare(editor, kODBlackBoxHandlerOfLastResort, length) == 0);
  105. }
  106.  
  107. //------------------------------------------------------------------------------
  108. // IsViewerOfLastResort
  109. //------------------------------------------------------------------------------
  110.  
  111. ODBoolean IsViewerOfLastResort(Environment* ev, ODPart* part)
  112. {
  113.     WASSERT(part);
  114.  
  115.     ODBoolean result = kODFalse;
  116.     
  117.     if (part)
  118.     {
  119.         TempODEditor editor = ((ODPartWrapper*)part)->GetEditor(ev);
  120.         
  121.         result =  IsViewerOfLastResort(ev,editor);
  122.     }
  123.  
  124.     return result;
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128. // IsViewerOfLastResort
  129. //------------------------------------------------------------------------------
  130.  
  131. ODBoolean IsViewerOfLastResort(Environment* ev, ODEditor editor)
  132. {
  133.     WASSERT(editor);
  134.     
  135.     ODBoolean result = kODFalse;
  136.     
  137.     if (editor)
  138.         result = GetgBinding()->EditorSupportsKind(ev,editor,kODViewerOfLastResortKind);
  139.     
  140.     return result;
  141. }
  142.  
  143. //------------------------------------------------------------------------------
  144. // ODSetPreferredEditorProps
  145. //------------------------------------------------------------------------------
  146.  
  147. void ODSetPreferredEditorProps(ODStorageUnit* thePartSU, ODEditor theChosenEditor )
  148. {
  149.     Environment* ev = somGetGlobalEnvironment();
  150.     
  151.     WASSERTM(thePartSU,         "Not setting prefered editor props");
  152.     WASSERTM(theChosenEditor,     "Not setting prefered editor props");
  153.  
  154.     if(thePartSU && theChosenEditor)
  155.     {
  156.         // If draft is read-only we cannot write anything to the storage unit.
  157.         ODBoolean readOnlyStorage = ( ODGetDraft(ev, thePartSU)->GetPermissions(ev) < kODDPSharedWrite );
  158.         ODBoolean viewerOfLastResort = IsViewerOfLastResort(ev,theChosenEditor);
  159.     
  160.         if (!readOnlyStorage && !viewerOfLastResort)
  161.         {
  162.             LOG("Setting kODPropPreferredEditor to %s", theChosenEditor);
  163.     
  164.             if (!thePartSU->Exists(ev, kODPropPreferredEditorUserString,kODMacIText,0))
  165.             {
  166.                 ODSUAddPropValue(ev, thePartSU, kODPropPreferredEditorUserString, kODMacIText);
  167.                 
  168.                 LOG("Created kODPropPreferredEditorUserString");
  169.             }
  170.             
  171.             ODSetISOStrProp(ev, thePartSU, kODPropPreferredEditor, 
  172.                                                     kODEditor, theChosenEditor);            
  173.             ODIText name = {0,0,0};
  174.             
  175.             TRY
  176.             
  177.             if (GetgBinding()->GetUserStringForEditor( ev, theChosenEditor, &name))
  178.             {
  179.                 // LOG("Setting kODPropPreferredEditorUserString to %s",
  180.                 // ((ODTradITextData*)name.text._buffer)->theText); //not null terminated!
  181.     
  182.                 ODSetITextProp(ev, thePartSU, kODPropPreferredEditorUserString, 
  183.                                                     kODMacIText, &name);
  184.             }
  185.             else
  186.                 WARN("Could not find user string for %s",theChosenEditor);
  187.                 
  188.             CATCH_ALL
  189.                 DisposeITextStruct(name);
  190.             ENDTRY
  191.             
  192.             DisposeITextStruct(name);
  193.  
  194.         }
  195.         else
  196.             LOG("Not setting prefered editor props");
  197.     }
  198. }
  199.  
  200. //------------------------------------------------------------------------------
  201. // ODSetPreferredKindProps
  202. //------------------------------------------------------------------------------
  203.  
  204. void ODSetPreferredKindProps(ODStorageUnit* thePartSU, ODType preferredkind)
  205. {
  206.     Environment* ev = somGetGlobalEnvironment();
  207.     
  208.     WASSERTM(thePartSU,         "Not setting prefered kind props");
  209.     WASSERTM(preferredkind,     "Not setting prefered kind props");
  210.  
  211.     if(thePartSU && preferredkind)
  212.     {
  213.         // If draft is read-only we cannot write anything to the storage unit.
  214.         ODBoolean readOnlyStorage = ( ODGetDraft(ev, thePartSU)->GetPermissions(ev) < kODDPSharedWrite );
  215.     
  216.         if (!readOnlyStorage)
  217.         {
  218.             LOG("Setting kODPropPreferredKind to %s", preferredkind);
  219.     
  220.             if (!thePartSU->Exists(ev, kODPropPreferredKindUserString,kODMacIText,0))
  221.             {
  222.                 ODSUAddPropValue(ev, thePartSU, kODPropPreferredKindUserString, kODMacIText);
  223.                 
  224.                 LOG("Created kODPropPreferredKindUserString");
  225.             }
  226.     
  227.             ODSetISOStrProp(ev, thePartSU, kODPropPreferredKind, 
  228.                                                      kODISOStr, preferredkind);    
  229.             ODIText name = {0,0,0};
  230.             
  231.             TRY
  232.             
  233.             if (GetgBinding()->GetUserStringForKind( ev, preferredkind, &name))
  234.             {
  235.                 // LOG("Setting kODPropPreferredKindUserString to %s",
  236.                 // ((ODTradITextData*)name.text._buffer)->theText); //not null terminated!
  237.     
  238.                 ODSetITextProp(ev, thePartSU, kODPropPreferredKindUserString, 
  239.                                                     kODMacIText, &name);
  240.             }
  241.             else
  242.                 WARN("Could not find user string for %s",preferredkind);
  243.                 
  244.             CATCH_ALL
  245.                 DisposeITextStruct(name);
  246.             ENDTRY
  247.             
  248.             DisposeITextStruct(name);
  249.  
  250.         }
  251.         else
  252.             LOG("Not setting prefered kind props");
  253.     }
  254. }
  255.  
  256. //------------------------------------------------------------------------------
  257. // GetCurrentEditorForPart
  258. //------------------------------------------------------------------------------
  259. ODEditor GetCurrentEditorForPart(ODPart* part )
  260. {
  261.     WASSERT(part);
  262.     
  263.     ODEditor editor = kODNULL;
  264.     
  265.     if (part)
  266.     {
  267.         Environment* ev = somGetGlobalEnvironment();
  268.     
  269.         editor = ((ODPartWrapper*)part)->GetEditor(ev);
  270.     }
  271.  
  272.     return editor;
  273. }
  274.  
  275. //------------------------------------------------------------------------------
  276. // ChangeEditor
  277. //------------------------------------------------------------------------------
  278.  
  279. void ChangeEditor(ODPart* part, ODEditor editorID)
  280. {
  281.     Environment* ev = somGetGlobalEnvironment();
  282.     ((ODPartWrapper*)part)->UseEditor(ev, editorID);
  283.     ODStorageUnit* thePartSU = part->GetStorageUnit(ev);
  284.     ODSetISOStrProp(ev, thePartSU, kODPropPreferredEditor, kODEditor, editorID);
  285. }
  286.  
  287. //------------------------------------------------------------------------------
  288. // ChangeKindAndEditor
  289. //------------------------------------------------------------------------------
  290.  
  291. void ChangeKindAndEditor (Environment *ev,
  292.         ODPart* part,
  293.         ODType newKind,
  294.         ODEditor newEditor,
  295.         ODNameSpaceManager* nsm)
  296. {
  297.     ODStorageUnit* partSU = part->GetStorageUnit(ev);    ODVolatile(partSU);
  298.     TempODEditor curEditor = GetCurrentEditorForPart(part);
  299.     TempODType curKind = ODGetKindFromPartSU(ev, partSU);
  300.     
  301.     if ( ODISOStrEqual(curEditor, newEditor) )
  302.     {
  303.         if ( !ODISOStrEqual(curKind, newKind) )
  304.             part->ChangeKind(ev, newKind);
  305.     }
  306.     else if ( partSU->Exists(ev, kODPropContents, newKind, 0) )
  307.     {
  308.         // Before changing the editor, ensure newKind is set as
  309.         // the preferred property
  310.         ODULong unused;
  311.         TempODType oldKind = ODGetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, kODNULL, &unused);
  312.         ODSetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, newKind);
  313.         TRY
  314.             ChangeEditor(part, newEditor);
  315.         CATCH_ALL
  316.             // If the editor didn't change, restore the preferred kind
  317.             TempODEditor failEditor = GetCurrentEditorForPart(part);
  318.             if ( ODISOStrEqual(failEditor, curEditor) )
  319.             {
  320.                 if ( (ODType) oldKind != kODNULL )
  321.                 {
  322.                     ODSetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, oldKind);
  323.                 }
  324.                 else
  325.                 {
  326.                     if ( ODSUExistsThenFocus(ev, partSU, kODPropPreferredKind, kODNULL) )
  327.                         partSU->Remove(ev);
  328.                 }
  329.             }
  330.             RERAISE;
  331.         ENDTRY
  332.     }
  333.     else if ( EditorSupportsKind(nsm, curEditor, newKind) )
  334.     {
  335.         // change kind first, then editor
  336.         part->ChangeKind(ev, newKind);
  337.  
  338.         // verify that the change took place before changing editor
  339.         TempODType tempKind = ODGetKindFromPartSU(ev,partSU);
  340.         if ( ODISOStrEqual(tempKind, newKind) )
  341.             ChangeEditor(part, newEditor);
  342.     }
  343.     else if ( EditorSupportsKind(nsm, newEditor, curKind) )
  344.     {
  345.         // change editor first, then kind
  346.         ChangeEditor( part, newEditor);
  347.         part->ChangeKind(ev, newKind);
  348.  
  349.         // if the kind didn't actually change, revert back to curEditor
  350.         TempODType tempKind = ODGetKindFromPartSU(ev,partSU);
  351.         if ( ODISOStrEqual(tempKind, curKind) )
  352.             ChangeEditor(part, curEditor);
  353.     }
  354.     else
  355.     {
  356.         // Internal error; editors do not support a common kind and
  357.         // translation has not been performed.
  358.         WARN("ChangeKindAndEditor cannot change kind or editor");
  359.     }
  360. }